#知乎登录 https://www.zhihu.com/#signin
#手机帐号

from lxml import etree
from urllib import request,parse
#xpath解析
import requests,time
#访问库
from PIL import Image
#图片解析库
#

class zhihudenglu(object):
	def __init__(self,name,password,session):
		print('该类配置中')
		self.name=name
		self.password=password
		self.session = session
		self._xsrf=etree.HTML(session.get('https://www.zhihu.com').text).xpath('/html/body/div[1]/div/div[2]/div[2]/form/input/@value')
		self.headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',"Host": "www.zhihu.com",
           "Referer": "https://www.zhihu.com/",
			'Accept-Encoding': 'gzip',
			'Host': 'www.zhihu.com',}



		#lifan=requests.get(url, headers=headers,timeout=30)
		#.xpath('/html/body/div[1]/div/div[2]/div[2]/form/input')

	def get_captcha(self):
		"""
	    获取验证码本地显示
	    返回你输入的验证码
	    """
		captcha_url = 'http://www.zhihu.com/captcha.gif?r=' + str(int(time.time() * 1000)) + "&type=login"
		request.urlretrieve(captcha_url, 'cptcha.jpg')
		img = Image.open('cptcha.jpg')
		img.show()
		captcha = input('本次登录需要输入验证码：')

		return captcha


		return self.session.get(url,headers=self.headers)



	def run(self,session):
		data={	'_xsrf': self._xsrf,
                'password': self.password,
                'remember_me': 'true',
                'phone_num': self.name,
               	'captcha':self.get_captcha(),
				  'captcha_type': 'cn'
                }
		self.denglu_url = 'https://www.zhihu.com/login/phone_num'
		post_data = parse.urlencode(data).encode('utf-8')
		print(data)
		loginresponse = session.post(self.denglu_url, headers=self.headers, data=post_data)

		print(loginresponse.json())

	#b'password=likujy1995&captcha=5vnu&phone_num=15171475219&remember_me=true&_xsrf=%5B%27e327f7e7e37ff321761a2d92ca3daa3a%27%5D'
	#b'captcha=ykh5&phone_num=15171475219&_xsrf=5b5a2d63805feb6ab10acd1d5f5893f3&remember_me=true&password=likujy1995'

if __name__ == "__main__":
	name='15171475219'
	password='likujy1995'
	session = requests.Session()
	zhihu=zhihudenglu(name,password,session)
	zhihu.run(session)
	print('游戏结束')

